package com.lori.netshop.service.imp;

import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

import redis.clients.jedis.JedisCluster;

import com.fasterxml.jackson.databind.JavaType;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.lori.netshop.mapper.TbItemDescMapper;
import com.lori.netshop.mapper.TbItemMapper;
import com.lori.netshop.mapper.TbItemParamItemMapper;
import com.lori.netshop.mapper.TbItemParamMapper;
import com.lori.netshop.pojo.TbItem;
import com.lori.netshop.pojo.TbItemDesc;
import com.lori.netshop.pojo.TbItemParamItem;
import com.lori.netshop.pojo.TbItemParamItemExample;
import com.lori.netshop.pojo.TbItemParamItemExample.Criteria;
import com.lori.netshop.service.ItemService;
import com.lori.netshop.util.NetshopResult;
import com.lori.netshop.util.StringUtil;
@Service
public class ItemServiceimp implements ItemService {
	@Autowired
	private TbItemMapper tbItemMapper;
	@Autowired
	private TbItemDescMapper tbItemDescMapper;
	@Autowired
	private TbItemParamItemMapper tbItemParamItemMapper;
	@Autowired
	private JedisCluster rediscolony;
	
	@Value("${ITEM_IN_REDIS_TIME}")
	private int ITEM_IN_REDIS_TIME;
	@Value("${REDIS_ITEM}")
	private String REDIS_ITEM;
	
	/**
	 * 根据商品ID获得商品
	 */
	@Override
	public NetshopResult findItem(long ID) {
		//redis获得数据
		try {
			String json = rediscolony.get(REDIS_ITEM+":"+ID+":base");
			if(!StringUtil.isEmpty(json)){
				ObjectMapper mapper = new ObjectMapper();
				TbItem readValue = mapper.readValue(json, TbItem.class);
				return NetshopResult.ok(readValue);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//获取数据
		TbItem item = tbItemMapper.selectByPrimaryKey(ID);
		//redis存入数据
		try {
			//判断是否有数据
			if(item != null){
				//将数据转成json
				ObjectMapper mapper = new ObjectMapper();
				String json = mapper.writeValueAsString(item);
				rediscolony.set(REDIS_ITEM+":"+ID+":base", json);
				//设置时间
				rediscolony.expire(REDIS_ITEM+":"+ID+":base", ITEM_IN_REDIS_TIME);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return NetshopResult.ok(item);
	}
	/**
	 * 根据商品ID获得商品详细信息
	 */
	@Override
	public NetshopResult findItemDesc(long ID) {
		//redis获得数据
		try {
			String json = rediscolony.get(REDIS_ITEM+":"+ID+":desc");
			if(!StringUtil.isEmpty(json)){
				ObjectMapper mapper = new ObjectMapper();
				TbItemDesc readValue = mapper.readValue(json, TbItemDesc.class);
				return NetshopResult.ok(readValue);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//获取数据
		TbItemDesc itemdesc = tbItemDescMapper.selectByPrimaryKey(ID);
		//redis存入数据
		try {
			//判断是否有数据
			if(itemdesc != null){
				//将数据转成json
				ObjectMapper mapper = new ObjectMapper();
				String json = mapper.writeValueAsString(itemdesc);
				rediscolony.set(REDIS_ITEM+":"+ID+":desc", json);
				//设置时间
				rediscolony.expire(REDIS_ITEM+":"+ID+":desc", ITEM_IN_REDIS_TIME);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		
		return NetshopResult.ok(itemdesc);
	}
	/**
	 * 根据商品ID获得商品规格参数
	 */
	@Override
	public NetshopResult findItemParam(long ID) {
		//redis获得数据
		try {
			String json = rediscolony.get(REDIS_ITEM+":"+ID+":param");
			if(!StringUtil.isEmpty(json)){
				ObjectMapper mapper = new ObjectMapper();
				TbItemParamItem readValue = mapper.readValue(json, TbItemParamItem.class);
				return NetshopResult.ok(readValue);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		//获取数据
		TbItemParamItem tbItemParamitem = null;
		TbItemParamItemExample example = new TbItemParamItemExample();
		Criteria criteria = example.createCriteria();
		criteria.andItemIdEqualTo(ID);
		List<TbItemParamItem> itemparamitem = tbItemParamItemMapper.selectByExampleWithBLOBs(example);
		if(itemparamitem != null && itemparamitem.size() > 0){
			tbItemParamitem = itemparamitem.get(0);
		}
		//redis存入数据
		try {
			//判断是否有数据
			if(tbItemParamitem != null){
				//将数据转成json
				ObjectMapper mapper = new ObjectMapper();
				String json = mapper.writeValueAsString(tbItemParamitem);
				rediscolony.set(REDIS_ITEM+":"+ID+":param", json);
				//设置时间
				rediscolony.expire(REDIS_ITEM+":"+ID+":param", ITEM_IN_REDIS_TIME);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return NetshopResult.ok(tbItemParamitem);
	}

}
